{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 根据hw-snp提取相关snp数据并向量化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "患病样本基因组数据文件总数: 7\n",
      "正常样本基因组数据文件总数: 11\n",
      "总样本基因组数据文件数: 18\n",
      "患病样本文件名 ['fix-data/AD\\\\006_S_4153.csv', 'fix-data/AD\\\\006_S_4192.csv', 'fix-data/AD\\\\018_S_4733.csv', 'fix-data/AD\\\\019_S_4252.csv', 'fix-data/AD\\\\019_S_4477.csv', 'fix-data/AD\\\\019_S_5012.csv', 'fix-data/AD\\\\019_S_5019.csv']\n",
      "正常样本文件名 ['fix-data/HC\\\\002_S_4213.csv', 'fix-data/HC\\\\002_S_4225.csv', 'fix-data/HC\\\\002_S_4262.csv', 'fix-data/HC\\\\002_S_4264.csv', 'fix-data/HC\\\\002_S_4270.csv', 'fix-data/HC\\\\006_S_4150.csv', 'fix-data/HC\\\\006_S_4357.csv', 'fix-data/HC\\\\006_S_4449.csv', 'fix-data/HC\\\\006_S_4485.csv', 'fix-data/HC\\\\012_S_4026.csv', 'fix-data/HC\\\\013_S_4579.csv']\n",
      "总样本文件名 ['006_S_4153', '006_S_4192', '018_S_4733', '019_S_4252', '019_S_4477', '019_S_5012', '019_S_5019', '002_S_4213', '002_S_4225', '002_S_4262', '002_S_4264', '002_S_4270', '006_S_4150', '006_S_4357', '006_S_4449', '006_S_4485', '012_S_4026', '013_S_4579']\n"
     ]
    }
   ],
   "source": [
    "import glob, os\n",
    "from os import getcwd\n",
    "from os import listdir\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "ad_files_num=len(os.listdir('fix-data/AD'))\n",
    "hc_files_num=len(os.listdir('fix-data/HC'))\n",
    "print(\"患病样本基因组数据文件总数:\",ad_files_num)\n",
    "print(\"正常样本基因组数据文件总数:\",hc_files_num)\n",
    "files_num=ad_files_num+hc_files_num\n",
    "print(\"总样本基因组数据文件数:\",files_num) \n",
    "adfiles = glob.glob(os.path.join('fix-data/AD/*.csv'))\n",
    "hcfiles = glob.glob(os.path.join('fix-data/HC/*.csv'))\n",
    "print (\"患病样本文件名\",adfiles)\n",
    "print (\"正常样本文件名\",hcfiles)\n",
    "allfiles = adfiles+hcfiles\n",
    "allfiles_name=[]\n",
    "for filepath in allfiles:\n",
    "    allfiles_name.append(filepath.split('.')[0].split('\\\\')[1])\n",
    "print (\"总样本文件名\",allfiles_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 单文件处理测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CHR</th>\n",
       "      <th>SNP</th>\n",
       "      <th>BP</th>\n",
       "      <th>A1</th>\n",
       "      <th>F_A</th>\n",
       "      <th>F_U</th>\n",
       "      <th>A2</th>\n",
       "      <th>CHISQ</th>\n",
       "      <th>P</th>\n",
       "      <th>OR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>rs10005853</td>\n",
       "      <td>0</td>\n",
       "      <td>T</td>\n",
       "      <td>0.28570</td>\n",
       "      <td>0.31820</td>\n",
       "      <td>G</td>\n",
       "      <td>0.042500</td>\n",
       "      <td>0.8367</td>\n",
       "      <td>0.8571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>rs10015934</td>\n",
       "      <td>0</td>\n",
       "      <td>T</td>\n",
       "      <td>0.35710</td>\n",
       "      <td>0.31820</td>\n",
       "      <td>C</td>\n",
       "      <td>0.058440</td>\n",
       "      <td>0.8090</td>\n",
       "      <td>1.1900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>rs1004236</td>\n",
       "      <td>0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.14290</td>\n",
       "      <td>0.27270</td>\n",
       "      <td>G</td>\n",
       "      <td>0.834900</td>\n",
       "      <td>0.3609</td>\n",
       "      <td>0.4444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>rs10059646</td>\n",
       "      <td>0</td>\n",
       "      <td>T</td>\n",
       "      <td>0.21430</td>\n",
       "      <td>0.22730</td>\n",
       "      <td>C</td>\n",
       "      <td>0.008349</td>\n",
       "      <td>0.9272</td>\n",
       "      <td>0.9273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>rs10155688</td>\n",
       "      <td>0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.07143</td>\n",
       "      <td>0.09091</td>\n",
       "      <td>G</td>\n",
       "      <td>0.042500</td>\n",
       "      <td>0.8367</td>\n",
       "      <td>0.7692</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CHR         SNP  BP A1      F_A      F_U A2     CHISQ       P      OR\n",
       "0    0  rs10005853   0  T  0.28570  0.31820  G  0.042500  0.8367  0.8571\n",
       "1    0  rs10015934   0  T  0.35710  0.31820  C  0.058440  0.8090  1.1900\n",
       "2    0   rs1004236   0  A  0.14290  0.27270  G  0.834900  0.3609  0.4444\n",
       "3    0  rs10059646   0  T  0.21430  0.22730  C  0.008349  0.9272  0.9273\n",
       "4    0  rs10155688   0  A  0.07143  0.09091  G  0.042500  0.8367  0.7692"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_hw =  pd.read_csv(\"hw-data/gwassnp.csv\",header=0,index_col=False,delim_whitespace = True)#从0行开始算，重新设置一列成为index值\n",
    "df_hw.head(5)#delim_whitespace = True必填 表示以空格作为定界符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CHR</th>\n",
       "      <th>SNP</th>\n",
       "      <th>BP</th>\n",
       "      <th>A1</th>\n",
       "      <th>F_A</th>\n",
       "      <th>F_U</th>\n",
       "      <th>A2</th>\n",
       "      <th>CHISQ</th>\n",
       "      <th>P</th>\n",
       "      <th>OR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>rs1042882</td>\n",
       "      <td>0</td>\n",
       "      <td>G</td>\n",
       "      <td>0.14290</td>\n",
       "      <td>0.54550</td>\n",
       "      <td>A</td>\n",
       "      <td>5.835</td>\n",
       "      <td>0.01571</td>\n",
       "      <td>0.1389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0</td>\n",
       "      <td>rs1062731</td>\n",
       "      <td>0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.28570</td>\n",
       "      <td>0.04545</td>\n",
       "      <td>G</td>\n",
       "      <td>4.129</td>\n",
       "      <td>0.04215</td>\n",
       "      <td>8.4000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0</td>\n",
       "      <td>rs11090516</td>\n",
       "      <td>0</td>\n",
       "      <td>T</td>\n",
       "      <td>0.28570</td>\n",
       "      <td>0.04545</td>\n",
       "      <td>C</td>\n",
       "      <td>4.129</td>\n",
       "      <td>0.04215</td>\n",
       "      <td>8.4000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0</td>\n",
       "      <td>rs11147179</td>\n",
       "      <td>0</td>\n",
       "      <td>A</td>\n",
       "      <td>0.28570</td>\n",
       "      <td>0.04545</td>\n",
       "      <td>C</td>\n",
       "      <td>4.129</td>\n",
       "      <td>0.04215</td>\n",
       "      <td>8.4000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>0</td>\n",
       "      <td>rs11832440</td>\n",
       "      <td>0</td>\n",
       "      <td>C</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.36360</td>\n",
       "      <td>T</td>\n",
       "      <td>6.545</td>\n",
       "      <td>0.01052</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568195</th>\n",
       "      <td>25</td>\n",
       "      <td>rs6588908</td>\n",
       "      <td>2060692</td>\n",
       "      <td>T</td>\n",
       "      <td>0.42860</td>\n",
       "      <td>0.13640</td>\n",
       "      <td>G</td>\n",
       "      <td>3.896</td>\n",
       "      <td>0.04840</td>\n",
       "      <td>4.7500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568206</th>\n",
       "      <td>25</td>\n",
       "      <td>rs35805291</td>\n",
       "      <td>2175375</td>\n",
       "      <td>T</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.27270</td>\n",
       "      <td>C</td>\n",
       "      <td>4.582</td>\n",
       "      <td>0.03231</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568262</th>\n",
       "      <td>25</td>\n",
       "      <td>rs17808080</td>\n",
       "      <td>2591888</td>\n",
       "      <td>T</td>\n",
       "      <td>0.07143</td>\n",
       "      <td>0.40910</td>\n",
       "      <td>A</td>\n",
       "      <td>4.862</td>\n",
       "      <td>0.02745</td>\n",
       "      <td>0.1111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568275</th>\n",
       "      <td>25</td>\n",
       "      <td>rs3763368</td>\n",
       "      <td>2638112</td>\n",
       "      <td>A</td>\n",
       "      <td>0.42860</td>\n",
       "      <td>0.09091</td>\n",
       "      <td>C</td>\n",
       "      <td>5.644</td>\n",
       "      <td>0.01752</td>\n",
       "      <td>7.5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568324</th>\n",
       "      <td>25</td>\n",
       "      <td>rs1973880</td>\n",
       "      <td>155230350</td>\n",
       "      <td>T</td>\n",
       "      <td>0.35710</td>\n",
       "      <td>0.09091</td>\n",
       "      <td>C</td>\n",
       "      <td>3.872</td>\n",
       "      <td>0.04911</td>\n",
       "      <td>5.5560</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30103 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        CHR         SNP         BP A1      F_A      F_U A2  CHISQ        P  \\\n",
       "8         0   rs1042882          0  G  0.14290  0.54550  A  5.835  0.01571   \n",
       "12        0   rs1062731          0  A  0.28570  0.04545  G  4.129  0.04215   \n",
       "23        0  rs11090516          0  T  0.28570  0.04545  C  4.129  0.04215   \n",
       "27        0  rs11147179          0  A  0.28570  0.04545  C  4.129  0.04215   \n",
       "51        0  rs11832440          0  C  0.00000  0.36360  T  6.545  0.01052   \n",
       "...     ...         ...        ... ..      ...      ... ..    ...      ...   \n",
       "568195   25   rs6588908    2060692  T  0.42860  0.13640  G  3.896  0.04840   \n",
       "568206   25  rs35805291    2175375  T  0.00000  0.27270  C  4.582  0.03231   \n",
       "568262   25  rs17808080    2591888  T  0.07143  0.40910  A  4.862  0.02745   \n",
       "568275   25   rs3763368    2638112  A  0.42860  0.09091  C  5.644  0.01752   \n",
       "568324   25   rs1973880  155230350  T  0.35710  0.09091  C  3.872  0.04911   \n",
       "\n",
       "            OR  \n",
       "8       0.1389  \n",
       "12      8.4000  \n",
       "23      8.4000  \n",
       "27      8.4000  \n",
       "51      0.0000  \n",
       "...        ...  \n",
       "568195  4.7500  \n",
       "568206  0.0000  \n",
       "568262  0.1111  \n",
       "568275  7.5000  \n",
       "568324  5.5560  \n",
       "\n",
       "[30103 rows x 10 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#筛选p<0.05的的数据\n",
    "\n",
    "df_hw.loc[df_hw[\"P\"]<0.05, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SNP Name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>rs1042882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>rs1062731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>rs11090516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>rs11147179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>rs11832440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568195</th>\n",
       "      <td>rs6588908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568206</th>\n",
       "      <td>rs35805291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568262</th>\n",
       "      <td>rs17808080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568275</th>\n",
       "      <td>rs3763368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568324</th>\n",
       "      <td>rs1973880</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30103 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          SNP Name\n",
       "8        rs1042882\n",
       "12       rs1062731\n",
       "23      rs11090516\n",
       "27      rs11147179\n",
       "51      rs11832440\n",
       "...            ...\n",
       "568195   rs6588908\n",
       "568206  rs35805291\n",
       "568262  rs17808080\n",
       "568275   rs3763368\n",
       "568324   rs1973880\n",
       "\n",
       "[30103 rows x 1 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_hw_new = pd.DataFrame()\n",
    "df_hw_new[\"SNP Name\"]=df_hw.loc[df_hw[\"P\"]<0.05, :][\"SNP\"]\n",
    "df_hw_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SNP Name</th>\n",
       "      <th>Allele1 - Forward</th>\n",
       "      <th>Allele2 - Forward</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>rs1000000</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>rs1000002</td>\n",
       "      <td>A</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rs10000023</td>\n",
       "      <td>T</td>\n",
       "      <td>T</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rs1000003</td>\n",
       "      <td>A</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>rs10000030</td>\n",
       "      <td>G</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730520</th>\n",
       "      <td>VGXS34742</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730521</th>\n",
       "      <td>VGXS34743</td>\n",
       "      <td>G</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730522</th>\n",
       "      <td>VGXS34744</td>\n",
       "      <td>G</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730523</th>\n",
       "      <td>VGXS34761</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730524</th>\n",
       "      <td>VGXS35706</td>\n",
       "      <td>T</td>\n",
       "      <td>T</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>730525 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          SNP Name Allele1 - Forward Allele2 - Forward\n",
       "0        rs1000000                 T                 C\n",
       "1        rs1000002                 A                 G\n",
       "2       rs10000023                 T                 T\n",
       "3        rs1000003                 A                 G\n",
       "4       rs10000030                 G                 G\n",
       "...            ...               ...               ...\n",
       "730520   VGXS34742                 C                 C\n",
       "730521   VGXS34743                 G                 G\n",
       "730522   VGXS34744                 G                 G\n",
       "730523   VGXS34761                 C                 C\n",
       "730524   VGXS35706                 T                 T\n",
       "\n",
       "[730525 rows x 3 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_snp =  pd.read_csv(\"fix-data/AD/006_S_4153.csv\",header=0,index_col=False)#从0行开始算，重新设置一列成为index值\n",
    "df_snp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SNP Name</th>\n",
       "      <th>Allele1 - Forward</th>\n",
       "      <th>Allele2 - Forward</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>rs1042882</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>rs1062731</td>\n",
       "      <td>A</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rs11090516</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rs11147179</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>rs11832440</td>\n",
       "      <td>T</td>\n",
       "      <td>T</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30098</th>\n",
       "      <td>rs6588908</td>\n",
       "      <td>T</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30099</th>\n",
       "      <td>rs35805291</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30100</th>\n",
       "      <td>rs17808080</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30101</th>\n",
       "      <td>rs3763368</td>\n",
       "      <td>A</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30102</th>\n",
       "      <td>rs1973880</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30103 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         SNP Name Allele1 - Forward Allele2 - Forward\n",
       "0       rs1042882                 A                 A\n",
       "1       rs1062731                 A                 G\n",
       "2      rs11090516                 T                 C\n",
       "3      rs11147179                 A                 A\n",
       "4      rs11832440                 T                 T\n",
       "...           ...               ...               ...\n",
       "30098   rs6588908                 T                 G\n",
       "30099  rs35805291                 C                 C\n",
       "30100  rs17808080                 A                 A\n",
       "30101   rs3763368                 A                 C\n",
       "30102   rs1973880                 T                 C\n",
       "\n",
       "[30103 rows x 3 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd_vector = pd.merge(df_hw_new, df_snp,on='SNP Name', how='inner')\n",
    "pd_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SNP Name</th>\n",
       "      <th>Allele1 - Forward</th>\n",
       "      <th>Allele2 - Forward</th>\n",
       "      <th>snp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>rs1042882</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>rs1062731</td>\n",
       "      <td>A</td>\n",
       "      <td>G</td>\n",
       "      <td>A G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rs11090516</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>T C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rs11147179</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>rs11832440</td>\n",
       "      <td>T</td>\n",
       "      <td>T</td>\n",
       "      <td>T T</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30098</th>\n",
       "      <td>rs6588908</td>\n",
       "      <td>T</td>\n",
       "      <td>G</td>\n",
       "      <td>T G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30099</th>\n",
       "      <td>rs35805291</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>C C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30100</th>\n",
       "      <td>rs17808080</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30101</th>\n",
       "      <td>rs3763368</td>\n",
       "      <td>A</td>\n",
       "      <td>C</td>\n",
       "      <td>A C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30102</th>\n",
       "      <td>rs1973880</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>T C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30103 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         SNP Name Allele1 - Forward Allele2 - Forward  snp\n",
       "0       rs1042882                 A                 A  A A\n",
       "1       rs1062731                 A                 G  A G\n",
       "2      rs11090516                 T                 C  T C\n",
       "3      rs11147179                 A                 A  A A\n",
       "4      rs11832440                 T                 T  T T\n",
       "...           ...               ...               ...  ...\n",
       "30098   rs6588908                 T                 G  T G\n",
       "30099  rs35805291                 C                 C  C C\n",
       "30100  rs17808080                 A                 A  A A\n",
       "30101   rs3763368                 A                 C  A C\n",
       "30102   rs1973880                 T                 C  T C\n",
       "\n",
       "[30103 rows x 4 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd_vector['snp']=pd_vector['Allele1 - Forward'].str.cat(pd_vector['Allele2 - Forward'],sep=' ')#拼接结果不添加\n",
    "pd_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SNP Name</th>\n",
       "      <th>Allele1 - Forward</th>\n",
       "      <th>Allele2 - Forward</th>\n",
       "      <th>snp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>rs1042882</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>rs1062731</td>\n",
       "      <td>A</td>\n",
       "      <td>G</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>rs11090516</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>rs11147179</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>rs11832440</td>\n",
       "      <td>T</td>\n",
       "      <td>T</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30098</th>\n",
       "      <td>rs6588908</td>\n",
       "      <td>T</td>\n",
       "      <td>G</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30099</th>\n",
       "      <td>rs35805291</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30100</th>\n",
       "      <td>rs17808080</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30101</th>\n",
       "      <td>rs3763368</td>\n",
       "      <td>A</td>\n",
       "      <td>C</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30102</th>\n",
       "      <td>rs1973880</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30103 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         SNP Name Allele1 - Forward Allele2 - Forward  snp\n",
       "0       rs1042882                 A                 A    0\n",
       "1       rs1062731                 A                 G   48\n",
       "2      rs11090516                 T                 C   96\n",
       "3      rs11147179                 A                 A    0\n",
       "4      rs11832440                 T                 T   80\n",
       "...           ...               ...               ...  ...\n",
       "30098   rs6588908                 T                 G  112\n",
       "30099  rs35805291                 C                 C  160\n",
       "30100  rs17808080                 A                 A    0\n",
       "30101   rs3763368                 A                 C   32\n",
       "30102   rs1973880                 T                 C   96\n",
       "\n",
       "[30103 rows x 4 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cllumn_name='snp'\n",
    "\n",
    "snp_AA= (pd_vector[cllumn_name]=='A A')\n",
    "snp_AT= (pd_vector[cllumn_name]=='A T')\n",
    "snp_AC= (pd_vector[cllumn_name]=='A C')\n",
    "snp_AG= (pd_vector[cllumn_name]=='A G')\n",
    "snp_TA= (pd_vector[cllumn_name]=='T A')\n",
    "snp_TT= (pd_vector[cllumn_name]=='T T')\n",
    "snp_TC= (pd_vector[cllumn_name]=='T C')\n",
    "snp_TG= (pd_vector[cllumn_name]=='T G')\n",
    "snp_CA= (pd_vector[cllumn_name]=='C A')\n",
    "snp_CT= (pd_vector[cllumn_name]=='C T')\n",
    "snp_CC= (pd_vector[cllumn_name]=='C C')\n",
    "snp_CG= (pd_vector[cllumn_name]=='C G')\n",
    "snp_GA= (pd_vector[cllumn_name]=='G A')\n",
    "snp_GT= (pd_vector[cllumn_name]=='G T')\n",
    "snp_GC= (pd_vector[cllumn_name]=='G C')\n",
    "snp_GG= (pd_vector[cllumn_name]=='G G')\n",
    "\n",
    "pd_vector.loc[snp_AA,cllumn_name] = 0\n",
    "pd_vector.loc[snp_AT,cllumn_name] = 16\n",
    "pd_vector.loc[snp_AC,cllumn_name] = 32\n",
    "pd_vector.loc[snp_AG,cllumn_name] = 48\n",
    "pd_vector.loc[snp_TA,cllumn_name] = 64\n",
    "pd_vector.loc[snp_TT,cllumn_name] = 80\n",
    "pd_vector.loc[snp_TC,cllumn_name] = 96\n",
    "pd_vector.loc[snp_TG,cllumn_name] = 112\n",
    "pd_vector.loc[snp_CA,cllumn_name] = 128\n",
    "pd_vector.loc[snp_CT,cllumn_name] = 144\n",
    "pd_vector.loc[snp_CC,cllumn_name] = 160\n",
    "pd_vector.loc[snp_CG,cllumn_name] = 176\n",
    "pd_vector.loc[snp_GA,cllumn_name] = 192\n",
    "pd_vector.loc[snp_GT,cllumn_name] = 208\n",
    "pd_vector.loc[snp_GC,cllumn_name] = 224\n",
    "pd_vector.loc[snp_GG,cllumn_name] = 240\n",
    "\n",
    "pd_vector.loc[:, cllumn_name] = pd_vector[cllumn_name].astype('int32')\n",
    "\n",
    "pd_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,  48,  96, ...,  80,  48,  80],\n",
       "       [160, 160, 160, ...,  48,  48,  48],\n",
       "       [  0,   0,   0, ..., 160, 160,  80],\n",
       "       ...,\n",
       "       [  0,   0,  80, ...,  80, 160,   0],\n",
       "       [ 80,  80,   0, ..., 160,   0,  32],\n",
       "       [ 96, 255, 255, ..., 255, 255, 255]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tensor_one=np.array(pd_vector['snp']).reshape(1,30103 )#转换为矩阵\n",
    "tensor_bmp=np.resize(tensor_one,(174,174))\n",
    "tensor_bmp[173,1:]=255#多余的赋值255\n",
    "tensor_bmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "\n",
    "cv2.imwrite(\"filename.png\", tensor_bmp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始样本批量处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图片已保存: 006_S_4153\n",
      "图片已保存: 006_S_4192\n",
      "图片已保存: 018_S_4733\n",
      "图片已保存: 019_S_4252\n",
      "图片已保存: 019_S_4477\n",
      "图片已保存: 019_S_5012\n",
      "图片已保存: 019_S_5019\n",
      "图片已保存: 002_S_4213\n",
      "图片已保存: 002_S_4225\n",
      "图片已保存: 002_S_4262\n",
      "图片已保存: 002_S_4264\n",
      "图片已保存: 002_S_4270\n",
      "图片已保存: 006_S_4150\n",
      "图片已保存: 006_S_4357\n",
      "图片已保存: 006_S_4449\n",
      "图片已保存: 006_S_4485\n",
      "图片已保存: 012_S_4026\n",
      "图片已保存: 013_S_4579\n"
     ]
    }
   ],
   "source": [
    "for filepath in allfiles:\n",
    "    df_hw =  pd.read_csv(\"hw-data/gwassnp.csv\",header=0,index_col=False,delim_whitespace = True)#从0行开始算，重新设置一列成为index值\n",
    "    df_hw_new = pd.DataFrame()\n",
    "    df_hw_new[\"SNP Name\"]=df_hw.loc[df_hw[\"P\"]<0.05, :][\"SNP\"]\n",
    "    df_snp =  pd.read_csv(filepath,header=0,index_col=False)#从0行开始算，重新设置一列成为index值\n",
    "    pd_vector = pd.merge(df_hw_new, df_snp,on='SNP Name', how='inner')\n",
    "    pd_vector['snp']=pd_vector['Allele1 - Forward'].str.cat(pd_vector['Allele2 - Forward'],sep=' ')#拼接结果不添加\n",
    "    cllumn_name='snp'\n",
    "    \n",
    "    snp_AA= (pd_vector[cllumn_name]=='A A')\n",
    "    snp_AT= (pd_vector[cllumn_name]=='A T')\n",
    "    snp_AC= (pd_vector[cllumn_name]=='A C')\n",
    "    snp_AG= (pd_vector[cllumn_name]=='A G')\n",
    "    snp_TA= (pd_vector[cllumn_name]=='T A')\n",
    "    snp_TT= (pd_vector[cllumn_name]=='T T')\n",
    "    snp_TC= (pd_vector[cllumn_name]=='T C')\n",
    "    snp_TG= (pd_vector[cllumn_name]=='T G')\n",
    "    snp_CA= (pd_vector[cllumn_name]=='C A')\n",
    "    snp_CT= (pd_vector[cllumn_name]=='C T')\n",
    "    snp_CC= (pd_vector[cllumn_name]=='C C')\n",
    "    snp_CG= (pd_vector[cllumn_name]=='C G')\n",
    "    snp_GA= (pd_vector[cllumn_name]=='G A')\n",
    "    snp_GT= (pd_vector[cllumn_name]=='G T')\n",
    "    snp_GC= (pd_vector[cllumn_name]=='G C')\n",
    "    snp_GG= (pd_vector[cllumn_name]=='G G')\n",
    "    \n",
    "    pd_vector.loc[snp_AA,cllumn_name] = 0\n",
    "    pd_vector.loc[snp_AT,cllumn_name] = 16\n",
    "    pd_vector.loc[snp_AC,cllumn_name] = 32\n",
    "    pd_vector.loc[snp_AG,cllumn_name] = 48\n",
    "    pd_vector.loc[snp_TA,cllumn_name] = 64\n",
    "    pd_vector.loc[snp_TT,cllumn_name] = 80\n",
    "    pd_vector.loc[snp_TC,cllumn_name] = 96\n",
    "    pd_vector.loc[snp_TG,cllumn_name] = 112\n",
    "    pd_vector.loc[snp_CA,cllumn_name] = 128\n",
    "    pd_vector.loc[snp_CT,cllumn_name] = 144\n",
    "    pd_vector.loc[snp_CC,cllumn_name] = 160\n",
    "    pd_vector.loc[snp_CG,cllumn_name] = 176\n",
    "    pd_vector.loc[snp_GA,cllumn_name] = 192\n",
    "    pd_vector.loc[snp_GT,cllumn_name] = 208\n",
    "    pd_vector.loc[snp_GC,cllumn_name] = 224\n",
    "    pd_vector.loc[snp_GG,cllumn_name] = 240\n",
    "    \n",
    "    pd_vector.loc[:, cllumn_name] = pd_vector[cllumn_name].astype('int32')\n",
    "\n",
    "    tensor_one=np.array(pd_vector['snp']).reshape(1,30103 )#转换为矩阵\n",
    "    tensor_bmp=np.resize(tensor_one,(174,174))\n",
    "    tensor_bmp[173,1:]=255#多余的赋值255\n",
    "    if allfiles.index(filepath)<ad_files_num:\n",
    "        image_path='hw-data/AD/'+allfiles_name[allfiles.index(filepath)]+'.png'\n",
    "    else:\n",
    "        image_path='hw-data/HC/'+allfiles_name[allfiles.index(filepath)]+'.png'\n",
    "    cv2.imwrite(image_path, tensor_bmp)\n",
    "    print(\"图片已保存:\",allfiles_name[allfiles.index(filepath)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
